/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kallsonys.session;

import com.kallsonys.dao.OrderDAO;
import com.kallsonys.service.CityOrder;
import com.kallsonys.service.Order;
import com.kallsonys.service.OrderQuote;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.ejb.Stateless;
import javax.jws.WebParam;
import javax.sql.DataSource;

/**
 *
 * @author allan
 */
@WebService(serviceName = "OrdersService")
@Stateless()
public class Orders {

    private com.kallsonys.dao.OrderDAO dao;
    @Resource(mappedName = "jdbc/OMS", type = DataSource.class)
    DataSource dsOMS;

    @PostConstruct
    void postConstruct() {
        dao = new OrderDAO(dsOMS);
    }

    public long CreateOrder(@WebParam(name = "order") Order order) throws Exception {
        long item = 0;
        try {
            item = dao.CreateOrder(order);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public void UpdateOrder(@WebParam(name = "order") Order order) throws Exception {
        try {
            dao.UpdateOrder(order);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return;
    }
    
    public boolean UpdateOrderStatus(@WebParam(name = "idOrder") long idOrder, 
                                  @WebParam(name = "status") int status, 
                                  @WebParam(name = "estimatedDelivery") Date estimatedDelivery) throws Exception {
        try {
            dao.UpdateOrderStatus(idOrder,status,estimatedDelivery);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return true;
    }

    public Order getById(@WebParam(name = "idOrder") long idOrder) throws Exception {
        Order item = null;
        try {
            item = dao.getById(idOrder);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public long CreateOrderQuote(@WebParam(name = "orderQuote") OrderQuote orderQuote) throws Exception {
        long item = 0;
        try {
            item = dao.CreateOrderQuote(orderQuote);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }

    public void UpdateOrderQuote(@WebParam(name = "orderQuote") OrderQuote orderQuote) throws Exception {
        try {
            dao.UpdateOrderQuote(orderQuote);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return;
    }

    public List<OrderQuote> getOrderQuoteByIdOrder(@WebParam(name = "idOrder") long idOrder) throws Exception {
        List<OrderQuote> item = null;
        try {
            item = dao.getOrderQuoteById(idOrder);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }
    
    public List<Order> getOpenOrdersByIdCustomer(@WebParam(name = "idCustomer")long idCustomer) throws Exception {
        List<Order> item = null;
        try {
            item = dao.getOpenOrdersByIdCustomer(idCustomer);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }
    
    public List<Order> getAllOpenOrders(@WebParam(name = "dummy")boolean dummy) throws Exception {
        List<Order> item = null;
        try {
            item = dao.getOpenOrders();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }
    
    public CityOrder getCityByOrder(long orderId) throws Exception {
         CityOrder item = null;
        try {
            item = dao.getCityByOrder(orderId);
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
        return item;
    }
}
